﻿namespace WLFramework.Web.Binder
{
    using System;
    using System.Runtime.Serialization;
    using WLFramework.Reflection.Metadata;

    [Serializable]
    public sealed class ModelException : Exception
    {
        public ModelMemberInfo ErrorProperty { get; set; }
        public string DisplayError { get; set; }
        public ModelMetadata Metadata { get; set; }

        public ModelException(Exception ex = null) 
            : base(string.Empty, ex)
        {
        }

        public ModelException(
            ModelMemberInfo errorProperty,
            string modelError,
            ModelMetadata metadata,
            Exception ex = null)
            : base(string.Empty, ex)
        {
            this.ErrorProperty = errorProperty;
            this.DisplayError = modelError;
            this.Metadata = metadata;
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException("info");

            info.AddValue("DisplayError", DisplayError);

            base.GetObjectData(info, context);
        }
    }
}
